var SearchPopup=function()
	{
	this.GetArgument(arguments[0],{Id:null,SkinId:null,Timeout:0,Type:null,OnSearch:null,OnGetSearch:null,GetType:null,ValueName:'results',Query:{},SearchEmptyKey:null,IsInputWidth:1,IsShowNull:0,MarkClass:null,ClientPath:null});
	this.OnSearch=fastCSharp.Event().Add(this.OnSearch);
	this.OnGetSearch=fastCSharp.Event().Add(this.OnGetSearch);
	this.SetLeftTopFunction=fastCSharp.ThisFunction(this,this.SetLeftTop);
//	this.$(document.body).AddEvent('mouseover',fastCSharp.ThisEvent(this,this.MouseOver)).AddEvent('mouseout',fastCSharp.ThisEvent(this,this.MouseOut)).AddEvent('click',fastCSharp.ThisEvent(this,this.ClickItem));
	this.Index=-1;
	this.Values={};
	};
SearchPopup.Inherit(fastCSharp.BaseFunction,{
MoveIndex:function(Count)
	{
	if(this.Value)
		{
		var Index=this.Index+Count;
		if(Index>=this.Value[this.ValueName].length)	Index=this.Value[this.ValueName].length-1;
		else if(Index<-1)	Index=-1;
		if(Index-this.Index)	this.SetIndex(Index);
		}
	},
KeyEnter:function()
	{
	if(this.Index+1)
		{
		var Event=new fastCSharp.Functions.BrowserEvent();
		Event.srcElement=this.$Id(this.Type+'SearchItem_'+this.Index).Element();
		this.ClickItem(Event);
		return true;
		}
	},
ClickItem:function(Event)
	{
	var Element=Event.Element('Name','searchItem');
	if(Element&&Event.Element('Id',this.SkinId))
		{
		var Link=new fastCSharp.Functions.Element('*searchHref',Element.Element()).Get('href');
		if(Link.substring(0,11).toLowerCase()=='javascript:')	eval(Link.substring(11));
		else	location=Link;
		}
	},
GetClass:function(Name,IsSet)
	{
//	if(Name.substring(Name.length-2)=='On')
//		{
//		if(!IsSet)	return Name.substring(0,Name.length-2);
//		}
//	else if(IsSet)	return Name+'On';
	return Name;
	},
SetIndex:function(Index)
	{
	this.ResetIndex();
	if((this.Index=Index)+1)
		{
		var Item=this.$Id(this.Type+'SearchItem_'+Index);
		Item.Class(this.GetClass(Item.Class(),1));
		}
	},
MouseOver:function(Event)
	{
	var Element=Event.Element('Name','searchItem');
	if(Element&&Event.Element('Id',this.SkinId))	this.SetIndex(parseInt(Element.Id().split('_')[1]));
	},
MouseOut:function(Event)
	{
	if(Event.Element('Name','searchItem')&&Event.Element('Id',this.SkinId))	this.ResetIndex();
	},
ResetIndex:function()
	{
	if(this.Index+1)
		{
		var Item=this.$Id(this.Type+'SearchItem_'+this.Index);
		Item.Class(this.GetClass(Item.Class(),0));
		}
	this.Index=-1;
	},
Search:function(Key)
	{
	if(Key=='')	Key=this.SearchEmptyKey;
	if(Key!=null)
		{
		var LowerKey=(Key=Key.substring(0,30)).toLowerCase(),Value=this.Values[LowerKey];
		if(Value)
			{
			if(Value!=1)
				{
				Value.key=Key;
				this.ShowValue(Value);
				}
			}
		else	{
			this.Values[LowerKey]=1;
			this.Query.key=Key;
			this.Query.onlyPath=0;
			this.OnSearch();
			LoadAjax.Get(fastCSharp.ThisFunction(this,this.GetSearch,[LowerKey]),'/include/'+this.GetType+'.html',this.Query);
			}
		}
	else	this.Hide(Key);
	},
GetSearch:function(Parameter,LowerKey)
	{
	var Values=Parameter[this.ValueName];
	this.OnGetSearch(Values,Parameter);
	for(var Index=Values.length;--Index>=0;SearchPopup.Mark(Value,this.MarkClass))
		{
		var Value=Values[Index];
		if(this.ClientPath)
			{
			var Name=Value.SearchType;
			if(Name=='Topic')
				{
				if(Value['LinkTopic'])	Name='LinkTopic';
				}
			else if(Name=='UserRemark')	Name='User';
			var Item=Value[Name]=fastCSharp.Copy({},Value[Name]);
			(Item.Path=fastCSharp.Copy({},Item.Path)).Index='javascript:'+this.ClientPath+'('+Item.Id+');';
			}
		}
	this.ShowValue(this.Values[Parameter.key.toLowerCase()]=Parameter);
	},
SearchPath:function(Key)
	{
	var LowerKey=Key.toLowerCase(),Value=this.Values[LowerKey];
	if(Value)	this.GetSearchPath(Value,Value);
	else	{
		this.Query.key=Key;
		this.Query.onlyPath=1;
		LoadAjax.Get(fastCSharp.ThisFunction(this,this.GetSearchPath),'/include/'+this.GetType+'.html',this.Query);
		}
	},
GetSearchPath:function(Parameter)
	{
	if(Parameter&&Parameter.searchPath)	location=Parameter.searchPath;
	},
ShowValue:function(Value)
	{
	if(this.Value!=Value)
		{
		if((this.Value=Value)[this.ValueName].length||this.IsShowNull)
			{
			fastCSharp.Skins[this.SkinId].Show(Value);
			if(this.IsInputWidth)	this.$Id(this.SkinId).Css('width',this.$Id(this.Id).Width()+'px');
			this.SetLeftTop();
			if(this.Timeout)	this.Interval=setInterval(this.SetLeftTopFunction,this.Timeout);
			this.Index=-1;
			}
		else	this.Hide();
		}
	},
SetLeftTop:function()
	{
	var XY=this.GetXY();
	this.$Id(this.SkinId).Css('position','absolute').ToXY(XY?XY.Left:0,XY?XY.Top:0).Css('zIndex',fastCSharp.ZIndex+1);
	},
GetItem:function(IsValue)
	{
	return IsValue?this.Value[this.ValueName].First(IsValue):(this.Index+1?this.Value[this.ValueName][this.Index]:null);
	},
Hide:function()
	{
	fastCSharp.Skins[this.SkinId].Hide();
	clearInterval(this.Interval);
	this.Index=-1;
	this.Value=this.Interval=null;
	}
		});
fastCSharp.Object.push(SearchPopup);
SearchPopup.Get=function(Argument)
	{
	if(!this.SearchPopups)	this.SearchPopups={};
	var Value=this.SearchPopups[Argument.SkinId];
	if(!Value)	this.SearchPopups[Argument.SkinId]=Value=new SearchPopup(Argument);
	return Value;
	};
SearchPopup.MarkClass='Red';
SearchPopup.Mark=function(Value,MarkClass)
	{
	for(var Indexs=[],IndexHash={},Index=Value.Indexs.length;--Index>=0;)
		{
		var StartIndex=Value.Indexs[Index],Length=Value.Lengths[Index];
		if(IndexHash[StartIndex]==null)
			{
			Indexs.push(StartIndex);
			IndexHash[StartIndex]=Length;
			}
		else if(IndexHash[StartIndex]<Length)	IndexHash[StartIndex]=Length;
		}
	Indexs.sort(function(Left,Right){return Left-Right;});
	switch(Value.SearchType)
		{
		case 'User':	Value.Text=Value.User.Name;	break;
		case 'UserRemark':	Value.Text=Value.User.Remark;	break;
		case 'Topic':	Value.Text=Value.Topic.Name;	break;
		case 'Question':	Value.Text=Value.Question.Title;	break;
		case 'Favorite':	Value.Text=Value.Favorite.Name;	break;
		}
	for(var Text=[],StartIndex=0,Index=0;Index!=Indexs.length;++Index)
		{
		if(Indexs[Index]>=StartIndex)
			{
			Text.push(Value.Text.substring(StartIndex,Indexs[Index]).ToHTML());
			Text.push('<span class="'+(MarkClass||SearchPopup.MarkClass)+'">'+Value.Text.substring(Indexs[Index],StartIndex=Indexs[Index]+IndexHash[Indexs[Index]]).ToHTML()+'</span>');
			}
		}
	Text.push(Value.Text.substring(StartIndex).ToHTML());
	Value.Text=Text.join('');
	};